<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>入库申请查询</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link rel="stylesheet" href="../../../lib/layui-v2.5.5/css/layui.css" media="all">
	</head>
	<body>
		<div class="layui-row" id="rkwul" style="display:none;">
			<div class="layui-col-md10" align="center">
				<form class="layui-form layui-from-pane" style="margin-top: 20px;" method="" id="accountFomat">
					<table id="rkweltable" lay-filter="test"></table>
					<div class="layui-form-item" style="margin-top:40px">
					</div>
				</form>
			</div>
		</div>
		</div>
		<div class="layui-row" id="xg" style="display:none;">
			<div class="layui-col-md10" align="center" style="width: 100%;">
				<form class="layui-form layui-from-pane" style="margin-top: 20px;" method="" id="accountFomat">
					<div class="layui-form-item" style="margin-top:40px">
						<form class="layui-form" action="" style="padding-top: 1%;">
							<div class="layui-form-item">
								<div class="layui-input-block" style="display: inline-block;margin-left: 0%;vertical-align: top;line-height: 40px;">
									入库人：&nbsp;&nbsp;<label id="name" style=""></label>
								</div>
								<div style="display: inline-block;margin-left: 0%;vertical-align: top;line-height: 40px;">
									<div style="display: inline-block;vertical-align: top;" id="citydiv">
										入库理由：&nbsp;&nbsp;<label id="labelreasonxg"></label>
									</div>

								</div>
								<div style="display: inline-block;" class="layui-btn-group demoTable">
									<div style="vertical-align: bottom;margin-top: 7px;">
										<button type="button" data-type="msg" id="tocke" class="layui-btn layui-btn-xs" style="vertical-align: top;">入库物料</button>
									</div>
								</div>
								<div class="layui-input-block" style="margin-left: 0;">
									<table id="ProductFile" lay-filter="ProductFile">
									</table>
								</div>
								<div class="layui-input-block" style="margin-left: 0;">
									<div style="display: inline-block;vertical-align: top;">
										<span style="font-size: 14px;">备注:</span>
									</div>
									<div style="display: inline-block;width: 40%;">
										<textarea name="reasonexact" id="reasonexact" required placeholder="请输入" class="layui-textarea"></textarea>
									</div>
								</div>
								<div class="layui-form-item">
									<div class="layui-input-block">
										<button class="layui-btn" lay-submit lay-filter="formDemo2">立即提交</button>
										<button type="reset" class="layui-btn layui-btn-primary">重置</button>
									</div>
								</div>
							</div>
							</from>
					</div>
				</form>
			</div>
		</div>
		</div>
		<div class="layui-row" id="update" style="display:none;">
			<div class="layui-col-md10" align="center" style="width: 100%;">
				<form class="layui-form layui-from-pane" style="margin-top: 20px;" method="" id="accountFomat">
					<div style="text-align: left;">
						入库人：&nbsp;&nbsp;<label id="labelname" style="margin-right: 5%;"></label>
						入库理由：&nbsp;&nbsp;<label id="labelreason"></label>
					</div>
					<table id="text" lay-filter="text"></table>
					<div class="layui-form-item" style="margin-top:40px">
						总件数：&nbsp;&nbsp;<label id="labelnumber" style="margin-right: 5%;"></label>
						总金额：&nbsp;&nbsp;<label id="labelprice"></label>
					</div>
				</form>
			</div>
		</div>
		<div>
			<form class="layui-form" action="">
				<div style="display: inline-block;margin-top: 1%;">
					<div style="display: inline-block;">
						<label>入库编号：</label>
					</div>
					<div style="display: inline-block;">
						<input class="layui-input" id="name1" type="text" />
					</div>
					<div style="display: inline-block;">
						<div style="display: inline-block;vertical-align: bottom;">
							<label class="layui-form-label">审核状态:</label>
						</div>
						<div style="display: inline-block;">
							<select name="city" id="city">
								<option value="">请选择</option>
								<option value="S001-1">审核通过</option>
								<option value="S001-2">审核不通过</option>
							</select>
						</div>

					</div>
					<div style="display: inline-block;" class="layui-btn-group demoTable">
						<button class="layui-btn" lay-submit lay-filter="formDemo">查询</button>
					</div>
				</div>
				</from>
				<div>
					<table id="table" lay-filter="table"></table>
				</div>
				<div class="layui-row" id="update" style="display:none;">
					<div class="layui-col-md10" align="center" style="width: 100%;">
						<form class="layui-form layui-from-pane" style="margin-top: 20px;" method="" id="accountFomat">
							<div style="text-align: left;">
								入库人：&nbsp;&nbsp;<label id="labelname" style="margin-right: 5%;"></label>
								入库理由：&nbsp;&nbsp;<label id="labelreason"></label>
							</div>
							<table id="text" lay-filter="text"></table>
							<div class="layui-form-item" style="margin-top:40px">
								总件数：&nbsp;&nbsp;<label id="labelnumber" style="margin-right: 5%;"></label>
								总金额：&nbsp;&nbsp;<label id="labelprice"></label>
							</div>
						</form>
					</div>
				</div>
		</div>
	</body>

	<script src="../../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
	<script src="../../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
	<script src="../../../lib/common/jquery.cookie.min.js" charset="utf-8"></script>
	<script src="../../../lib/common/common.js" charset="utf-8"></script>
	<script type="text/javascript">
		layui.use(["form", "table", 'jquery', 'laydate'], function() {
				var data1=new Array();
			var pot;
			var Detailslist = new Array();
			var table = layui.table,
				layer = layui.layer,
				form = layui.form,
				laydate = layui.laydate,
				$ = layui.jquery;
			table.render({
				elem: '#table',
				id: 'idTest',
				url: api8 + "PutRepertory/PutRepertorychecker2",
				page: true, //开启分页
				cols: [
					[ //标题栏
						{
							checkbox: true
						}, {
							field: 'gatherId',
							title: '订单编号'
						}, {
							field: 'storer',
							title: '入库人'
						}, {
							field: 'reason',
							title: '入库理由',
							templet: '#reason'
						}, {
							field: 'registerTime',
							title: '复核时间'
						}, {
							field: 'amountSum',
							title: '总件数'
						}, {
							field: 'costPriceSum',
							title: '总金额'
						}, {
							field: 'checkTag',
							title: '审核状态',
							templet: '#checkTag'
						}, {
							field: '',
							title: '操作',
							templet: '#formulate',
							width: 260
						}
					]
				]
			});
			$('.demoTable .layui-btn').on('click', function() {
				var type = $(this).data('type');
				active[type] ? active[type].call(this) : '';
			});
			var $ = layui.$,
				active = {
					namesel: function() { //获取选中数目

					},
					msg: function() { //验证是否全选
						layer.open({
							type: 1,
							title: '入库物料',
							area: ['900px', '600px'],
							content: $('#rkwul') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
						});
						table.render({
							elem: '#rkweltable',
							width: 900,
							url: api8 + 'RepertoryCell/getRepertroyCellList' //数据接口
								,
							page: true //开启分页
								,
							cols: [
								[ //表头
									{
										field: 'productId',
										title: '产品编号',
										width: 200,
										sort: true,
										fixed: 'left'
									}, {
										field: 'productName',
										title: '产品名称',
										width: 120
									}, {
										field: 'type',
										title: '用途类型',
										width: 120,
										templet: '#forType'
									}, {
										field: 'personalUnit',
										title: '单位',
										width: 120
									}, {
										field: 'costPrice',
										title: '	成本单价（元）',
										width: 180
									}, {
										field: 'city',
										title: '操作',
										width: 150,
										templet: '#formulate2'
									}
								]
							],
							done: function(res, curr, count) {
								//如果是异步请求数据方式，res即为你接口返回的信息。
								//如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
								for(var i=0;i<res.data.length;i++){
									res.data[i].amount=0;
								}
							}
						});
					}
				};
				//查询
			form.on('submit(formDemo)', function(data) {
				layer.msg($("#name1").val())
				table.render({
					elem: '#table',
					id: 'idTest',
					url: api8 + "PutRepertory/PutRepertorychecker2?name=" + $("#name1").val() + "&check=" + data.field.city,
					cols: [
						[ //标题栏
							{
								checkbox: true
							}, {
								field: 'gatherId',
								title: '订单编号'
							}, {
								field: 'storer',
								title: '入库人'
							}, {
								field: 'reason',
								title: '入库理由',
								templet: '#reason'
							}, {
								field: 'registerTime',
								title: '复核时间'
							}, {
								field: 'amountSum',
								title: '总件数'
							}, {
								field: 'costPriceSum',
								title: '总金额'
							}, {
								field: 'checkTag',
								title: '审核状态',
								templet: '#checkTag'
							}, {
								field: '',
								title: '操作',
								templet: '#formulate',
								width: 260
							}
						]
					]
				});

				return false;
			});
			//修改提交
			form.on('submit(formDemo2)', function(data) {
				
				console.log(Detailslist)
				for (var i = 0; i < Detailslist.length; i++) {
					if(typeof(Detailslist[i].amount)=="undefined"||Detailslist[i].amount==""||Detailslist[i].amount==0){
						layer.msg("数量不能为空！",{icon:5})
						return false;
					}
					if(isNaN(Detailslist[i].amount)){
						layer.msg("数量格式不正确！",{icon:5})
						return false;
					}
				}
				if(Detailslist[0]==""){
						console.log(Detailslist.shift())
				}
				for (var i = 0; i < Detailslist.length; i++) {
							Detailslist[i].id="";
							Detailslist[i].reason=pot.reason;
							Detailslist[i].parentId=pot.id;
				}
				console.log(Detailslist)
				$.ajax({
				            url: api8+"PutRepertoryDetails/PutRepertoryDetailsreupdate",
				            type: "Post",
				            data: JSON.stringify(Detailslist),
				            dataType:"JSON",
				            contentType:'application/json;charset=utf-8',
				            success: function (data) {
								layer.msg("执行成功！",{icon:1});
								return true;
				            },
				            error: function (data) {
				                console.log(data.status);
				            }
				        });
				return false;
			});

			table.on('tool(table)', function(obj) {
				var putr = new Array();
				var data = obj.data;
				putr.push(data);
				if (obj.event === 'detail') {
					layer.open({
						type: 1,
						title: '查看详情',
						area: ['900px', '600px'],
						content: $('#update') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
					});
					if (data.reason == "R001-1") {
						$("#labelreason").html("生产入库")
					} else if (data.reason == "R001-2") {
						$("#labelreason").html("库存初始")
					} else if (data.reason == "R001-3") {
						$("#labelreason").html("赠送")
					} else if (data.reason == "R001-4") {
						$("#labelreason").html("内部归还")
					}
					$("#labelname").html(data.storer)
					$("#labelnumber").html(data.amountSum)
					$("#labelprice").html(data.costPriceSum)
					table.init('text', {
						url: api8 + "PutRepertoryDetails/PutRepertoryDetailsid?id=" + data.id,
						page: true, //开启分页
						cols: [
							[{
								field: 'productId',
								sort: true,
								title: '产品编号'
							}, {
								field: 'productName',
								title: '物料名称'
							}, {
								field: 'productDescribe',
								title: '描述'
							}, {
								field: 'amount',
								title: '数量',
							}, {
								field: 'amountUnit',
								title: '单位',
							}, {
								field: 'costPrice',
								title: '	成本单价（元）',
							}, {
								field: 'subtotal',
								title: '	小计（元）',
							}]
						]
					});
				}  else if (obj.event === 'edit') {
				console.log(data)
					layer.open({
						type: 1,
						title: '修改',
						area: ['900px', '600px'],
						content: $('#xg') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
					});
					if (data.reason == "R001-1") {
						$("#labelreasonxg").html("生产入库")
					} else if (data.reason == "R001-2") {
						$("#labelreasonxg").html("库存初始")
					} else if (data.reason == "R001-3") {
						$("#labelreasonxg").html("赠送")
					} else if (data.reason == "R001-4") {
						$("#labelreasonxg").html("内部归还")
					}
					form.render();
					$("#name").html(data.storer)
					$("#labelnumber").html(data.amountSum)
					$("#reasonexact").val(data.reasonexact)
					$("#labelprice").html(data.costPriceSum)

					table.init('ProductFile', {
						url: api8 + "PutRepertoryDetails/PutRepertoryDetailsid?id=" + data.id,
						totalRow: true,

						page: true, //开启分页
						cols: [
							[{
								field: 'productId',
								title: '产品编号',
								sort: true,
								totalRowText: '合计'
							}, {
								field: 'productName',
								title: '物料名称'
							}, {
								field: 'productDescribe',
								title: '描述'
							}, {
								field: 'amount',
								title: '数量',
								edit: 'text'
							}, {
								field: 'amountUnit',
								title: '单位',
							}, {
								field: 'costPrice',
								title: '	成本单价（元）',
							}, {
								field: 'subtotal',
								title: '	小计（元）',
								totalRow: true
							}, {
								field: 'city',
								title: '操作',
								width: 150,
								templet: '#formulate3'
							}]
						],
						done: function(res, curr, count) {
							//如果是异步请求数据方式，res即为你接口返回的信息。
							//如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
							//得到当前页码
							Detailslist = res.data;
								pot=data;
								console.log(Detailslist)
							$.ajax({
								url: api8 + "RepertoryCell/getRepertroyCellList",
								type: "Post",
								dataType: "JSON",
								contentType: 'application/json;charset=utf-8',
								success: function(data) {
									data1=data.data
									return true;
								},
								error: function(data) {
								}
							});
						}
					});
				}
			});
			table.on('tool(test)', function(obj) {
				var ok;
				var data = obj.data;
				if (Detailslist.length != 0) {
					for (var i = 0; i < Detailslist.length; i++) {
						if (Detailslist[i].productId == data.productId) {
							layer.msg("请勿重复添加", {
								icon: 5
							})
							ok = 0;
							break;
							return;
						} else {
							ok = 1;
						}
						//if(ab[i].id)
					}
				} else {
					obj.data.subtotal = 0
					Detailslist[0] = data;
				}
				if (ok == 1) {
					obj.data.subtotal = 0
					Detailslist[Detailslist.length] = data
				}
				table.init('ProductFile', {
					totalRow: true,
				
					page: true, //开启分页
					cols: [
						[{
							field: 'productId',
							title: '产品编号',
							sort: true,
							totalRowText: '合计'
						}, {
							field: 'productName',
							title: '物料名称'
						}, {
							field: 'productDescribe',
							title: '描述'
						}, {
							field: 'amount',
							title: '数量',
							edit: 'text'
						}, {
							field: 'amountUnit',
							title: '单位',
						}, {
							field: 'costPrice',
							title: '	成本单价（元）',
						}, {
							field: 'subtotal',
							title: '	小计（元）',
							totalRow: true
						}, {
							field: 'city',
							title: '操作',
							width: 150,
							templet: '#formulate3'
						}]
					],
					data:Detailslist
				});
			});
			//修改删除
			table.on('tool(ProductFile)', function(obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
				var data = obj.data; //获得当前行数据
				var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
				var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
				layer.confirm('真的删除行么', function(index) {
					obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
					layer.close(index);
					//向服务端发送删除指令
					// console.log(data)
					// console.log(layEvent)
					// console.log(ab)
					//array.splice(start,1)
					for (var i = 0; i < Detailslist.length; i++) {
			
						if (Detailslist[i].productId == data.productId) {
							Detailslist.splice(i, 1)
							console.log(Detailslist);
							table.init('ProductFile', {
								page: true, //开启分页
								totalRow: true,
								cols: [
									[{
										field: 'productId',
										title: '产品编号',
										sort: true,
										totalRowText: '合计'
									}, {
										field: 'productName',
										title: '物料名称'
									}, {
										field: 'productDescribe',
										title: '描述'
									}, {
										field: 'amount',
										title: '数量',
										edit: 'text'
									}, {
										field: 'amountUnit',
										title: '单位',
									}, {
										field: 'costPrice',
										title: '	成本单价（元）',
									}, {
										field: 'subtotal',
										title: '	小计（元）',
										totalRow: true
									}, {
										field: 'city',
										title: '操作',
										width: 150,
										templet: '#formulate3'
									}]
								],
								data: Detailslist
							});
							break;
						}
					}
				});
			
			});
			table.on('edit(ProductFile)', function(obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
					for (var i = 0; i < data1.length; i++) {
						if(obj.data.productId==data1[i].productId){
							if((parseInt(obj.value)+parseInt(data1[i].amount))>data1[i].maxCapacityAmount){
								formpd=0;
								layer.msg("超出最大库存！！,最大库存:"+data1[i].maxCapacityAmount+",原有库存:"+data1[i].amount,{icon:5})
								return;
							}
							if(obj.value>data1[i].maxCapacityAmount){
								formpd=0;
								layer.msg("超出最大库存！,最大库存:"+data1[i].maxCapacityAmount,{icon:5})
								return;
							}else if(obj.value<=0){
								layer.msg("请输入正确的数量",{icon:5});
								formpd=0;
								return;
							}
						}
					}
					formpd=1;
				for (var i = 0; i < Detailslist.length; i++) {
					if (Detailslist[i].productId == obj.data.productId) {
						Detailslist[i].subtotal = obj.value * obj.data.costPrice;
						Detailslist[i].amount = obj.value;
						break;
					}
				}
				table.init('ProductFile', {
					page: true, //开启分页
					totalRow: true,
					cols: [
						[{
							field: 'productId',
							title: '产品编号',
							sort: true,
							totalRowText: '合计'
						}, {
							field: 'productName',
							title: '物料名称'
						}, {
							field: 'productDescribe',
							title: '描述'
						}, {
							field: 'amount',
							title: '数量',
							edit: 'text'
						}, {
							field: 'amountUnit',
							title: '单位',
						}, {
							field: 'costPrice',
							title: '	成本单价（元）',
						}, {
							field: 'subtotal',
							title: '	小计（元）',
							totalRow: true
						}, {
							field: 'city',
							title: '操作',
							width: 150,
							templet: '#formulate3'
						}]
					],
					data: Detailslist
				});
			});
		})
	</script>
	<script type="text/html" id="reason">
		{{# if(d.reason=='R001-1'){d.reason='生产入库'}}
						{{d.reason}}
					{{# }else if(d.reason=='R001-2'){d.reason='库存初始'}}
						{{d.reason}}
					{{# }else if(d.reason=='R001-3'){d.reason='赠送'}}
						{{d.reason}}
					{{# }else if(d.reason=='R001-4'){d.reason='内部归还'}}
						{{d.reason}}
					{{# }}}
		
	</script>
	<script type="text/html" id="checkTag">
		{{# if(d.checkTag=='S001-1'){d.checkTag='<span style="color:#00FF00">审核通过</span>'}}
						{{d.checkTag}}
					{{# }else{d.checkTag='<span style="color:red">审核不通过</span>'}}
						{{d.checkTag}}
					{{# }}}
		
	</script>
	<script type="text/html" id="formulate">
		{{# if(d.checkTag=='S001-1'){d.reason='生产入库'}}
				<button type="button" lay-event="detail" data-type="msg" class="layui-btn layui-btn-xs">查看详情</button>
					{{# }else{d.reason=''}}
				<button type="button" lay-event="detail" data-type="msg" class="layui-btn layui-btn-xs">查看详情</button>
				<button type="button" lay-event="edit" class="layui-btn layui-btn-xs">修改</button>
			{{# }}}
		
	</script>
	<script type="text/html" id="formulate3">
		<button type="button" class="layui-btn layui-btn-xs" id="a" lay-event="edit">删除</button> 
	</script>
	<script type="text/html" id="formulate2">
		<button type="button" class="layui-btn layui-btn-xs" id="a" lay-event="edit">入库</button> 
	</script>
	<script type="text/html" id="forType">
		{{# if(d.type=='Y001-1'){d.type='商品'}}
			{{d.type}}
		{{# }else{d.type='物料'}}
			{{d.type}}
		{{# }}}
	</script>
</html>
